package com.lz.thymeleaf.controller;


import com.lz.thymeleaf.pojo.Admin;
import com.lz.thymeleaf.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Slf4j
public class AdminController implements UserDetailsService {
    @Autowired
    private AdminService adminService;
    /**
     * 登录处理逻辑
     * 开启spring security后，会因为实现了UserDetailsService接口直接走这段逻辑
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws AuthenticationException {
          List<Admin> list = adminService.findAdmin(username);
          if (list == null){
              return null;
          }
        Admin admin = list.get(0);
        return new User(username, admin.getPassword(), true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("Admin"));

    }


//修改密码
    @RequestMapping("/changePassword")
    @ResponseBody
    public Map<String,Integer> changePwd(String oldpwd,String newpwd,String confirmpwd){
        //正则表达式
        String regex = "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}";
        String adminName = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Integer> data = new HashMap<>();
        if (!oldpwd.equals(adminService.findAdminPassword(adminName))){
            data.put("code",0);
            log.info("原密码不正确");
            return data;
        }else if (oldpwd.equals(newpwd)){
            data.put("code",1);
            log.info("新密码不能与原密码一样");
            return data;
        }else if (!newpwd.equals(confirmpwd)){
            data.put("code",2);
            log.info("两次密码输入不一致");
            return data;
        }else if (!newpwd.matches(regex)){
            data.put("code",3);
            log.info("密码格式错误");
            return data;
        }
        Admin admin = new Admin(adminName,newpwd);
        int result = adminService.changePwd(admin);
        if (result > 0) {
            data.put("code", 200);
            log.info(adminName+"----修改密码成功-----");
        } else {
            data.put("code",400);
            log.info(adminName+"----修改密码失败------");
        }
        return data;
    }
}
